package com.weiweb.dao;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.weiweb.domain.User;

public class UserDaoImpl extends HibernateDaoSupport implements UserDao {

	public User findUser(User user) {
		String hql = "from User u where u.userName=?";
		String[] param = new String[] { user.getUserName() };
		@SuppressWarnings("unchecked")
		List<User> list = getHibernateTemplate().find(hql, param);
		if (null == list || 0==list.size()) {
			return null;
		} else {
			return list.get(0);
		}
	}

	public User findSinaUserByUid(User user) {
		String queryString = "from User u where u.sinaUid=:sinaUid";
		String[] paramName = new String[] {"sinaUid"};
		String[] value = new String[] { user.getSinaUid() };
		@SuppressWarnings("unchecked")
		List<User> list = getHibernateTemplate().findByNamedParam(queryString,
				paramName, value);
		if (null == list || 0==list.size()) {
			return null;
		}else{
			User u = list.get(0);
			return u;
		}
	}

	public void insertUser(User user) {
		getHibernateTemplate().save(user);
	}


	public void updateUser(User user){
		getHibernateTemplate().update(user);
	}

	public static void main(String args[]) {
		ApplicationContext context = new FileSystemXmlApplicationContext(
				"classpath:applicationContext.xml");
		UserDao userDAO = (UserDao) context.getBean("userDao");
		User u = new User();
		u.setSinaUid("1375993057");
		u.setAccessToken("2.006eWHVBBP4GtDc7e0bd4edbsjAKkC");
		User myu = userDAO.findSinaUserByUid(u);
		System.out.println(myu.getId());
		System.out.println(myu.getSinaUid());
		System.out.println(myu.getAccessToken());

	}
}
